package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeExpertAdviceSummaryService;
import com.bidevalution.dao.entity.BeExpertAdviceSummary;
import com.bidevalution.dao.mapper.BeExpertAdviceSummaryMapper;
import com.qtp.common.constants.SystemConstants;
import com.qtp.core.model.Response;
import com.qtp.core.util.OConvertUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 查慧英
 * @date 2019-11-20 18:45
 */
@Service(value = "beExpertAdviceSummaryService")
public class BeExpertAdviceSummaryServiceImpl implements BeExpertAdviceSummaryService {

    private Logger logger = Logger.getLogger(getClass());
    private static final String UNPASSED_REASON = "unpassedReason";
    private static final String ENTERPRISE_NAME = "enterpriseName";
    private static final String SECTION_ID = "sectionId";
    private static final String LINT_FEED = "<br>";
    private static final String STOP_SIGN = "、";
    @Autowired(required = false)
    BeExpertAdviceSummaryMapper beExpertAdviceSummaryMapper;
    @Override
    public Response<BeExpertAdviceSummary> getBeExpertAdviceSummary(String sectionId) {
        Map<String, Object> params = new HashMap<>(2);
        params.put(SECTION_ID, sectionId);
        try {
            BeExpertAdviceSummary item = beExpertAdviceSummaryMapper.getBeExpertAdviceSummary(params);
            if (item == null) {
                item = new BeExpertAdviceSummary();
                item.setSectionId(sectionId);
                item.setSeqId(UUID.randomUUID().toString());
                List<Map<String, Object>> list = beExpertAdviceSummaryMapper.getUnpassedReasonList(params);
                StringBuilder unpassedReason = new StringBuilder();
                if (list != null && !list.isEmpty()) {
                    String reason;
                    int i = 1;
                    for (Map<String, Object> map : list) {
                        reason = OConvertUtils.getString(map.get(UNPASSED_REASON));
                        if (i != SystemConstants.NUM_1){
                            unpassedReason.append(LINT_FEED);
                        }
                        unpassedReason.append(i++).append(STOP_SIGN).append(reason);
                    }
                    item.setUnpassedInfo(unpassedReason.toString());
                }

                Map<String, Object> candidate = beExpertAdviceSummaryMapper.getCandidate(params);
                if (candidate != null) {
                    item.setCandidate(OConvertUtils.getString(candidate.get(ENTERPRISE_NAME)));
                }
                item.setCreateTime(new Date());
                beExpertAdviceSummaryMapper.create(item);
            }

            return Response.ok(item);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return Response.fail("操作失败");
        }

    }

    @Override
    public Response addOrUpdate(BeExpertAdviceSummary item) {
        BeExpertAdviceSummary summary = beExpertAdviceSummaryMapper.load(item.getSeqId());
        if (summary != null) {
            item.setUpdateTime(new Date());
            return Response.ok(beExpertAdviceSummaryMapper.update(item));
        } else {
            item.setCreateTime(new Date());
            return  Response.ok(beExpertAdviceSummaryMapper.create(item));
        }
    }
}
